package cn.lliiooll.mtlib.config;

import cn.lliiooll.mtlib.utils.StringUtil;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.lang.reflect.Field;
import java.util.List;

/**
 * 用于快速的加载配置文件
 */
public class BukkitConfig {


    public static <T> T load(FileConfiguration config, Class<T> clazz) throws Exception {
        return load(config, clazz, null);
    }

    public static <T> T load(FileConfiguration config, Class<T> clazz, String path) throws Exception {
        T t = clazz.newInstance();
        for (Field field : t.getClass().getDeclaredFields()) {
            if (field.getAnnotation(YamlField.class) != null) {
                // 设置字段可访问
                field.setAccessible(true);
                YamlField cf = field.getAnnotation(YamlField.class);
                String p = (StringUtil.isNullOrEmpty(path) ? "" : path + ".") + (!StringUtil.isNullOrEmpty(cf.name()) ? cf.name() : field.getName());
                if (
                        String.class.equals(field.getType()) ||
                                int.class.equals(field.getType()) ||
                                long.class.equals(field.getType()) ||
                                short.class.equals(field.getType()) ||
                                boolean.class.equals(field.getType()) ||
                                double.class.equals(field.getType()) ||
                                byte.class.equals(field.getType()) ||
                                float.class.equals(field.getType()) ||
                                char.class.equals(field.getType()) ||
                                Integer.class.equals(field.getType()) ||
                                Long.class.equals(field.getType()) ||
                                List.class.equals(field.getType())
                ) {
                    // 基本类型
                    field.set(t, config.get(p));
                } else {
                    // 自定义类
                    field.set(t, load(config, field.getType(), p));
                }
            }
        }
        return t;
    }

    public static void save(File file, Object object) {
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileConfiguration config = YamlConfiguration.loadConfiguration(file);
            save(config, object, null);
            config.save(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void save(FileConfiguration config, Object object, String path) throws Exception {
        for (Field field : object.getClass().getDeclaredFields()) {
            if (field.getAnnotation(YamlField.class) != null) {
                // 设置字段可访问
                field.setAccessible(true);
                YamlField cf = field.getAnnotation(YamlField.class);
                String p = (StringUtil.isNullOrEmpty(path) ? "" : path + ".") + (!StringUtil.isNullOrEmpty(cf.name()) ? cf.name() : field.getName());
                if (String.class.equals(field.getType()) ||
                        int.class.equals(field.getType()) ||
                        long.class.equals(field.getType()) ||
                        short.class.equals(field.getType()) ||
                        boolean.class.equals(field.getType()) ||
                        double.class.equals(field.getType()) ||
                        byte.class.equals(field.getType()) ||
                        float.class.equals(field.getType()) ||
                        char.class.equals(field.getType()) ||
                        Integer.class.equals(field.getType()) ||
                        Long.class.equals(field.getType()) ||
                        List.class.equals(field.getType())
                ) {
                    config.set(p, field.get(object));
                } else {
                    save(config, field.get(object), p);
                }
            }
        }
    }
}
